package com.guoqing.example.controller;

import com.guoqing.example.common.MyEnum;
import com.guoqing.example.exception.MyExceptionHandler;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author ZhangGuoQing
 * @Date 2020/8/19/0019 21:38
 */
@RestController
public class LoginController {

    @RequestMapping("/login")
    public String login(String username, String pwd) {
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            return "login success";
        } catch (AuthenticationException ex) {
            ex.printStackTrace();
            return "账号或密码错误！";
        } catch (AuthorizationException ex) {
            ex.printStackTrace();
            return "没有权限！";
        }
    }

    //注解验角色和权限
    @RequiresRoles("admin") // roleName
    @RequestMapping("/checkRole")
    public String index() {
        return "index!";
    }

    //注解验角色和权限
    @RequiresPermissions("add") // resourceName
    @RequestMapping("/checkResource")
    public String indexResource() {
        return "index!";
    }

    // 有 角色1，没有add权限
    @RequiresRoles("角色1") // roleName
    @RequiresPermissions("add") // resourceName
    @RequestMapping("/test1")
    public String test1() {
        return "test1当前登录用户拥有角色1, 没有菜单add";
    }

    @RequiresPermissions("菜单7") // resourceName
    @RequestMapping("/test2")
    public String test2() {
        return "test2当前登录用户 有菜单7";
    }

    @RequiresRoles("角色1") // roleName
    @RequiresPermissions("菜单2") // resourceName
    @RequestMapping("/test3")
    public String test3() {
        return "test3当前登录用户拥有角色1 和 菜单2";
    }

    @RequiresRoles("角色111") // roleName
    @RequiresPermissions("add") // resourceName
    @RequestMapping("/test111")
    public String test111() {
        return "test1当前登录用户拥有角色1, 没有菜单add";
    }


    @RequestMapping("/test4")
    public String test4() {
        try {
            int i = 1 / 0;
            return "test4";
        } catch (Exception ex) {
            throw new RuntimeException("test4分母不能为00000");
        }
    }

    @RequestMapping("/test5")
    public String test5() {
        try {
            int i = 1 / 0;
            return "test5";
        } catch (Exception ex) {
            throw new MyExceptionHandler(MyEnum.FAILED.getCode(), "test5分母不能为00000");
        }
    }

    @RequestMapping("/test6")
    public String test6() {
        int i = 1 / 0;
        return "test6";
    }

}
